package pt.compflow.tools;

import java.io.IOException;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggerOutputStream extends OutputStream {

	private static final Logger LOG = LoggerFactory.getLogger(LoggerOutputStream.class);
	
	private StringBuilder buffer;
	private boolean isError = false;
		
	public LoggerOutputStream(boolean isError) {
		super();
		this.isError = isError;
		buffer = new StringBuilder();
	}

	@Override
	public final void write(int b) throws IOException {
				
		if(((char) b) == '\n') {
			if(isError) LOG.error(buffer.toString()); else LOG.info(buffer.toString());
			buffer = new StringBuilder();
		}
		
		buffer.append((char) b);		
	}

}
